<?php

class Application_Model_Dao_DeliveryorderMapper extends Base_Mapper
{

	function __construct(){
		parent::__construct();
		$this->tableName = 'Application_Model_DbTable_Deliveryorder';
	}   
   
	public function save(Application_Model_Deliveryorder $do)
	{
		$data = array(
			'serial_number'   => $do->get_serialNumber(),
			'customer_id' => $do->get_customer(),
			'settlement_mode' => $do->get_settlementMode(),
			'invoice_status' => $do->get_invoiceStatus(),
			'entrepot_id' => $do->get_entrepot(),
			//'create_date' => $do->get_createDate(),
			'create_date' => date('Y-m-d H:i:s'),
			'expire_days' => $do->get_expireDays(),
			'total' => $do->get_total(),
		);
		$id = $do->get_id();
		if ($id === null || $id === "") {
			unset($data['id']);
			$data['serial_number'] = "dummy";
			$this->logger->debug(print_r($data,true));
			$record_id = $this->getDbTable()->insert($data);
			$data['id'] = $record_id;
			$data['serial_number'] = $record_id;
			$this->getDbTable()->update($data, array('id = ?' => $record_id));
			return $record_id;
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
			return $id;
		}
	}
   
	public function find($id)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$do = new Application_Model_BusiInfo_Deliveryorder($row->toArray());
		$customerInfo = new Application_Model_BusiInfo_Customer(array(
				'id' => $row['customer_id'],
		));	
		$do->__set('customerInfo',$customerInfo);
			
		$entrepotInfo = new Application_Model_BusiInfo_Entrepot(array(
			'id' => $row['entrepot_id'],
		));
		$do->__set('entrepotInfo',$entrepotInfo);
		return $do;
	}
   
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {	
			$this->logger->debug(print_r($row->toArray(),true));
			$entry = new Application_Model_BusiInfo_Deliveryorder($row->toArray());
			$entries[] = $entry;
		}
		return $entries;
	}
	
	public function getAllDeliveryOrder(){
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = 'SELECT a.*, b.[name] custmerName, c.[name] entrepotName'
			.' FROM deliveryorder a, customer b, entrepot c'
			.' WHERE a.customer_id = b.id'
			.' AND a.entrepot_id = c.id';
			
		$this->logger->debug(__METHOD__. '   ' .$sql);
		$resultSet = $db->fetchAll($sql);
		$this->logger->debug(print_r($resultSet, true));
		$entries   = array();
		foreach ($resultSet as $row) {
			$this->logger->debug(print_r($row, true));
//			$entry = new Application_Model_BusiInfo_Deliveryorder(array(
//				'id' => $row['id'],
//				'serialNumber' => $row['serial_number'],
//				'settlement_mode' => $row['settlement_mode'],
//				'invoiceStatus' => $row['invoice_status'],
//				'createDate' => $row['create_date'],
//				'expireDays' => $row['expire_days'],
//				'total' => $row['total'],
//			));
			$entry = new Application_Model_BusiInfo_Deliveryorder($row);
			
			$custInfo = new Application_Model_BusiInfo_Customer(array(
				'id' => $row['customer_id'],
				'name' => $row['custmerName'],			
			));
			$entry->__set('customerInfo',$custInfo);
			
			$entrepotInfo = new Application_Model_BusiInfo_Entrepot(array(
				'id' => $row['entrepot_id'],
				'name' => $row['entrepotName'],			
			));
			$entry->__set('entrepotInfo',$entrepotInfo);
			
			$entries[] = $entry;
		}
		return $entries;
		
	}
	
	
	private function toModel($record){
		$do = new Application_Model_BusiInfo_Deliveryorder(); 
		$do->set_id($record->id);
	    $do->set_serialNumber($record->serial_number);
	    $do->set_customer($record->customer_id);
	    $do->set_settlementMode($record->settlement_mode);
	    $do->set_invoiceStatus($record->invoice_status);
	    $do->set_entrepot($record->entrepot_id);
	    $do->set_createDate($record->create_date); 
	    $do->set_expireDays($record->expire_days);
	    $do->set_total($record->total); 
	
	    return $do;
		
	}
	
	public function delete($id) {		
		$this->getDbTable()->delete( "id=" . $id);
	}
}

